# -*- coding: utf-8 -*-
"""
Created on Sun Dec 15 15:41:29 2013

@author: Liang Zhang
"""
import numpy as np
import cfunc

def __subvec(data,block_size):
    _data=[]
    size=((data.shape[1]+block_size-1)/block_size)
    for x in data:
        t=x.copy()
        t.resize(size*block_size)
        _data.append(t)
    _data=np.array(_data,np.int)
#    sel=[]
#    for i in range(size):
#        sel.extend(range(i,size*block_size,size))
#    _data=_data[:,sel].copy()
    _data.shape=(size*data.shape[0],block_size)
    return _data

def OSHC(data,block_size=12,enc=32):
    vecs=__subvec(data,block_size)
    vecs=cfunc.v2i(vecs)
    code,Nenc=cfunc.OSHC(vecs,block_size,enc);
    from huffman import huffman
    h=huffman(Nenc)
    sum=0
    for i in range(len(h)):
        sum+=Nenc[i]*len(h[i])
    sum+=Nenc[len(Nenc)-1]*block_size
    return sum
            
if __name__=='__main__':
    from time import clock
    t0=clock()
    from testset import testset
    ts=testset('F:/work/vlsi-test/TestDataCompression/circutes/s9234')
    #ts=testset('s337')
    print ts.name
    n=OSHC(ts.data)
    print '%.2f'%((1-n*1.0/ts.data.size)*100)
    print clock()-t0
  
